<template>
<div class="container">
    <div class="card">
        <el-breadcrumb separator="/" class="card-body">
            <el-breadcrumb-item :to="{ path: '/AdminHome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: '/AssetsManagement' }">资产管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ this.$route.meta.title }}</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="card">
        <div class="card-header">填写资产信息</div>
        <div class="card-body">
            <el-form :model="assetForm" :rules="rules" ref="assetForm" label-width="120px">
                <div class="info-assetForm">
                    <el-form-item label="编号" prop="code">
                        <el-input v-model="assetForm.code" disabled placeholder="资产编号自动生成,无法修改"></el-input>
                    </el-form-item>
                    <el-form-item label="密级">
                        <el-input disabled placeholder="暂未评定资产密级"></el-input>
                    </el-form-item>
                    <el-form-item label="当前资产状态" prop="assetStatus">
                        <el-select v-model="assetForm.assetStatus" placeholder="资产状态">
                            <el-option :label="s.label" :value="s.code" v-for="s in StaConfig" :key="s.index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="资产名称" prop="name">
                        <el-input v-model="assetForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="责任人" prop="principalName">
                        <el-input v-model="principalName" ref="zzzzr2" placeholder="请选择分类"></el-input>
                        <el-button @click="$tree('查找责任人2','zzzzr2')" type="text" size="medium" class="el-query-tree">查找</el-button>
                    </el-form-item>
                    <el-form-item label="分类" prop="typeName">
                        <el-input v-model="typeName" ref="flxx" placeholder="请选择分类"></el-input>
                        <el-button @click="$tree('查找分类','flxx')" type="text" size="medium" class="el-query-tree">查找</el-button>
                    </el-form-item>
                    <el-form-item label="所属部门" prop="departmentName">
                        <el-input v-model="departmentName" ref="ccbm2" placeholder="请选择部门"></el-input>
                        <el-button @click="$tree('查找组织','ccbm2')" type="text" size="medium" class="el-query-tree">查找</el-button>
                    </el-form-item>
                    <el-form-item label="储存位置" prop="storageName">
                        <el-input v-model="storageName" ref="xzcccczz" placeholder="请选择储存位置"></el-input>
                        <el-button @click="$tree('查找储存位置','xzcccczz')" type="text" size="medium" class="el-query-tree">查找</el-button>
                    </el-form-item>
                    <el-form-item label="主MAC地址" prop="macAddress">
                        <el-input v-model="assetForm.macAddress" placeholder="例:'00-01-6C-06-A6-29''"></el-input>
                    </el-form-item>
                    <el-form-item label="主IP地址" prop="ipAddress">
                        <el-input v-model="assetForm.ipAddress" placeholder="例:'192.168.1.11''"></el-input>
                    </el-form-item>
                </div>
                <hr>
                <div>
                    <h3>所含设备列表</h3>
                    <el-table v-if="assetForm.assetsDeviceMappingList != ''" :data="assetForm.assetsDeviceMappingList" style="width: 100%">
                        <el-table-column prop="deviceCode" label="编号" width="120">
                        </el-table-column>
                        <el-table-column prop="deviceName" label="设备名称">
                        </el-table-column>
                        <el-table-column prop="propertyValue" label="设备规格">
                        </el-table-column>
                        <el-table-column prop="number" label="数量" width="180" align="center">
                          <template slot-scope="scope">
                            <el-input v-model="scope.row.number" class="number"></el-input>
                          </template>
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="120">
                          <template slot-scope="scope">
                            <el-button @click.native.prevent="deleteRow(scope.$index, assetForm.assetsDeviceMappingList)" type="text" size="small">
                            移除
                            </el-button>
                          </template>
                        </el-table-column>
                    </el-table>
                    <DevicePicker v-on:getDevice="getDevice"></DevicePicker>
                </div>
                <hr>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('assetForm')">立即保存</el-button>
                    <el-button @click="goBack()">返回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</div>
</template>

<script>
import request from "@/utils/request";
import config from "@/config/index";
import DevicePicker from "@/components/DevicePicker";

export default {
  name: "AssetsCreate",
  components: {
    DevicePicker
  },
  data() {
    /*
     自定义规格
    */
    var checkMacAdress = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('主Mac地址不能为空'));
      }else{
        let regexp = new RegExp("([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}")
        if(!regexp.test(value)){
          callback(new Error('Mac地址格式不正确!'));
        }else{
          callback();
        }
      }
    };
    var checkIpAdress = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('主IP地址不能为空'));
      }else{
        let regexp = new RegExp(/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/)
        if(!regexp.test(value)){
          callback(new Error('IP地址格式不正确!'));
        }else{
          callback();
        }
      }
    };
    return {
      StaConfig: "",
      show: false,
      assetForm: {
        assetStatus: "",
        assetsDeviceMappingList: [],
        code: "",
        departmentId: "",
        departmentName: "",
        id: "",
        ipAddress: "",
        macAddress: "",
        name: "",
        principalId: "",
        principalName: "",
        secretLevel: "",
        storageId: "",
        storageName: "",
        typeId: "",
        typeName: ""
      },
      
      rules: {
        name: [
          { required: true, message: "请输入资产名称", trigger: "change" },
          { min: 1, max: 30, message: "长度在 1 到 30 个字符", trigger: "change" }
        ],
        principalName: [
          { required: true, message: "请输入责任人姓名", trigger: "change" },
          { min: 1, max: 15, message: "长度在 1 到 15 个字符", trigger: "change" }
        ],
        typeName: [{ required: true, message: "请选择分类", trigger: "change" }],
        departmentName: [
          { required: true, message: "请选择部门", trigger: "change" }
        ],
        storageName: [
          { required: true, message: "请选择储存位置", trigger: "change" }
        ],
        macAddress:[
          { validator: checkMacAdress, trigger: 'change' }
        ],
        ipAddress:[
          { validator: checkIpAdress, trigger: 'change' }
        ]
      },
      imageUrl: "",
      dialogVisible: false,
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
            request({
              method: "post",
              url: this.$api.createAssets,
              data: this.assetForm
            })
              .then(res => {
                if (res.code == "000000") {
                  this.$message.success("保存成功!");
                  this.goBack();
                } else {
                  this.disabled = false;
                  return false;
                }
              })
              .finally(() => {
                this.disabled = false;
                return false;
              });
        } else {
          return false;
        }
      });
    },
    handleNodeClick(data) {
      console.log(data);
    },
    goBack() {
      this.$router.go(-1); //返回上一层
    },
    getgetGenerateCode() {
      //获取分类编码
      request({
        url: this.$api.getAssetsGenerateCode,
        method: "get"
      }).then(res => {
        this.assetForm.code = "ZZ" + res.data;
      });
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    getDevice(data) {
      console.log(data);
      this.assetForm.assetsDeviceMappingList.push(data);
    }
  },
  mounted() {
    this.getgetGenerateCode();
    this.StaConfig = config.DeviceStatus;
  },
  computed: {
    typeName: function() {
      if (this.$store.getters.treeData.ref !== "flxx") {
        return this.assetForm.typeName;
      } else {
        this.assetForm.typeId = this.$store.getters.treeData.id;
        this.assetForm.typeName = this.$store.getters.treeData.val;
        return this.$store.getters.treeData.val;
      }
    },
    departmentName: function() {
      if (this.$store.getters.treeData.ref !== "ccbm2") {
        return this.assetForm.departmentName;
      } else {
        this.assetForm.departmentId = this.$store.getters.treeData.id;
        this.assetForm.departmentName = this.$store.getters.treeData.val;
        return this.$store.getters.treeData.val;
      }
    },
    storageName: function() {
      if (this.$store.getters.treeData.ref !== "xzcccczz") {
        return this.assetForm.storageName;
      } else {
        this.assetForm.storageId = this.$store.getters.treeData.id;
        this.assetForm.storageName = this.$store.getters.treeData.val;
        return this.$store.getters.treeData.val;
      }
    },
    principalName: function() {
      if (this.$store.getters.treeData.ref !== "zzzzr2") {
        return this.assetForm.principalName;
      } else {
        this.assetForm.principalId = this.$store.getters.treeData.id;
        this.assetForm.principalName = this.$store.getters.treeData.val;
        return this.$store.getters.treeData.val;
      }
    },
  }
};
</script>
<style scoped>
.info-assetForm {
  width: 420px;
  position: relative;
}

.login-log {
  position: absolute;
  right: 0;
  top: 0;
  border-left: 1px solid #f6f6f6;
  border-bottom: 1px solid #f6f6f6;
  color: #999;
  border-radius: 0 0 0 3px;
}

.login-log p {
  padding: 5px 2rem;
  margin-bottom: 5px;
  margin-top: 0;
}

.login-log h4 {
  padding: 5px 2rem;
  margin-bottom: 5px;
  margin-top: 5px;
  font-size: 16px;
  color: #333;
}

.login-log hr {
  margin: 0;
}

.authList {
  border: 1px solid #f2f2f2;
  padding: 0.5rem 1rem;
}

.del-query-tree-btn {
  position: absolute;
  right: -40px;
  top: 0;
}
.number{
  width: 70px;
  margin: 0 auto;
  text-align: center;
}
</style>